package com.woniu.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/05 12:22
 */
public class CreateJWTDemo {
    private final static long EXPIRATION = 30L;

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        long ex = l + EXPIRATION * 1000;
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("woniuxy")
                .setSubject("管理员")
                .setIssuedAt(new Date())
                //签名算法
                .signWith(SignatureAlgorithm.HS256, "mayuhang")
                //过期时间：30s
                .setExpiration(new Date(ex))
                .claim("account", "admin")
                .claim("status", "Y");
        System.out.println(jwtBuilder.compact());
        System.out.println("--------------------------------");
        System.out.println(parse(jwtBuilder.compact()));
    }

    public static String parse(String jwt) {
        Claims mayuhang = Jwts.parser().setSigningKey("mayuhang").parseClaimsJws(jwt).getBody();
        return mayuhang.getId() + mayuhang.getIssuedAt()
                + mayuhang.get("account")
                + mayuhang.get("status")
                + mayuhang.getExpiration()
                + mayuhang.getSubject();
    }


}
